package com.kuaimacode.kframework.admin.services.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.vo.response.UserAccountDetailVO;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.user.UserAccountDetailMapper;
import com.kuaimacode.kframework.mybatis.models.user.UserAccountDetail;
import com.kuaimacode.kframework.mybatis.models.user.UserAccountDetailPO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.admin.services.user
 * @Description: created by IntelliJ IDEA
 * @date 2019-05-25 21:41
 */
@Service
public class UserAccountDetailService extends ServiceImpl<UserAccountDetailMapper, UserAccountDetail> {

    /**
     * 查询用户余额明细
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<UserAccountDetailVO> findUserBalanceDetailList(UserAccountDetailVO search, int pageNum, int pageSize) {
        search.setAccountType(Constant.AccountTypeEnum.ACCOUNT.getCode().toString());
        return findAccountDetailList(search, pageNum, pageSize);
    }

    /**
     * 查询充值记录明细
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<UserAccountDetailVO> findUserRechargeDetailList(UserAccountDetailVO search, int pageNum, int pageSize) {
        search.setAccountType(Constant.AccountTypeEnum.ACCOUNT.getCode().toString());
        search.setInOutType(Constant.InOutEnum.RECHARGE.getCode().toString());
        return findAccountDetailList(search, pageNum, pageSize);
    }

    private PageInfo<UserAccountDetailVO> findAccountDetailList(UserAccountDetailVO search, int pageNum, int pageSize) {
        UserAccountDetailPO searchPO = new UserAccountDetailPO();
        BeanUtils.copyProperties(search, searchPO);
        PageHelper.startPage(pageNum, pageSize);
        searchPO.setAccountType(Constant.AccountTypeEnum.ACCOUNT.getCode().toString());
        List<UserAccountDetailPO> list = baseMapper.findUserAccountDetailList(searchPO);
        if (CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(new ArrayList<>());
        }
        PageInfo pageInfo = PageInfo.of(list);
        pageInfo.setList(list.stream()
                .map(detailPO -> {
                    UserAccountDetailVO detailVO = new UserAccountDetailVO();
                    BeanUtils.copyProperties(detailPO, detailVO);
                    return detailVO;
                }).collect(Collectors.toList()));
        return pageInfo;
    }
}
